package com.yserp.kd.service.impl;
import com.yserp.kd.service.TWlApiServiceI;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import com.yserp.kd.entity.TWlApiEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.io.Serializable;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.jeecgframework.core.util.MyClassLoader;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.web.cgform.enhance.CgformEnhanceJavaInter;

@Service("tWlApiService")
@Transactional
public class TWlApiServiceImpl extends CommonServiceImpl implements TWlApiServiceI {

	
 	public void delete(TWlApiEntity entity) throws Exception{
 		super.delete(entity);
 		//执行删除操作增强业务
		this.doDelBus(entity);
 	}
 	
 	public Serializable save(TWlApiEntity entity) throws Exception{
 		Serializable t = super.save(entity);
 		//执行新增操作增强业务
 		this.doAddBus(entity);
 		return t;
 	}
 	
 	public void saveOrUpdate(TWlApiEntity entity) throws Exception{
 		super.saveOrUpdate(entity);
 		//执行更新操作增强业务
 		this.doUpdateBus(entity);
 	}
 	
 	/**
	 * 新增操作增强业务
	 * @param t
	 * @return
	 */
	private void doAddBus(TWlApiEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 更新操作增强业务
	 * @param t
	 * @return
	 */
	private void doUpdateBus(TWlApiEntity t) throws Exception{
		//-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	/**
	 * 删除操作增强业务
	 * @param id
	 * @return
	 */
	private void doDelBus(TWlApiEntity t) throws Exception{
	    //-----------------sql增强 start----------------------------
	 	//-----------------sql增强 end------------------------------
	 	
	 	//-----------------java增强 start---------------------------
	 	//-----------------java增强 end-----------------------------
 	}
 	
 	private Map<String,Object> populationMap(TWlApiEntity t){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id", t.getId());
		map.put("orderid", t.getOrderid());
		map.put("mailno", t.getMailno());
		map.put("is_gen_bill_no", t.getIsGenBillNo());
		map.put("jcompany", t.getJcompany());
		map.put("jcontact", t.getJcontact());
		map.put("jtel", t.getJtel());
		map.put("jmobile", t.getJmobile());
		map.put("jshippercode", t.getJshippercode());
		map.put("jcountry", t.getJcountry());
		map.put("jprovince", t.getJprovince());
		map.put("jcity", t.getJcity());
		map.put("jcounty", t.getJcounty());
		map.put("jaddress", t.getJaddress());
		map.put("jpost_code", t.getJpostCode());
		map.put("dcompany", t.getDcompany());
		map.put("dcontact", t.getDcontact());
		map.put("dtel", t.getDtel());
		map.put("dmobile", t.getDmobile());
		map.put("ddeliverycode", t.getDdeliverycode());
		map.put("dcountry", t.getDcountry());
		map.put("dprovince", t.getDprovince());
		map.put("dcity", t.getDcity());
		map.put("dcounty", t.getDcounty());
		map.put("daddress", t.getDaddress());
		map.put("dpost_code", t.getDpostCode());
		map.put("custid", t.getCustid());
		map.put("pay_method", t.getPayMethod());
		map.put("express_type", t.getExpressType());
		map.put("parcel_quantity", t.getParcelQuantity());
		map.put("cargo_length", t.getCargoLength());
		map.put("cargo_width", t.getCargoWidth());
		map.put("cargo_height", t.getCargoHeight());
		map.put("volume", t.getVolume());
		map.put("cargo_total_weight", t.getCargoTotalWeight());
		map.put("declared_value", t.getDeclaredValue());
		map.put("declared_value_currency", t.getDeclaredValueCurrency());
		map.put("customs_batchs", t.getCustomsBatchs());
		map.put("sendstarttime", t.getSendstarttime());
		map.put("is_docall", t.getIsDocall());
		map.put("need_return_tracking_no", t.getNeedReturnTrackingNo());
		map.put("return_tracking", t.getReturnTracking());
		map.put("d_tax_no", t.getDTaxNo());
		map.put("tax_pay_type", t.getTaxPayType());
		map.put("tax_set_accounts", t.getTaxSetAccounts());
		map.put("original_number", t.getOriginalNumber());
		map.put("payment_tool", t.getPaymentTool());
		map.put("payment_number", t.getPaymentNumber());
		map.put("goods_code", t.getGoodsCode());
		map.put("in_process_waybill_no", t.getInProcessWaybillNo());
		map.put("brand", t.getBrand());
		map.put("specifications", t.getSpecifications());
		map.put("temp_range", t.getTempRange());
		map.put("order_name", t.getOrderName());
		map.put("order_cert_type", t.getOrderCertType());
		map.put("order_cert_no", t.getOrderCertNo());
		map.put("order_source", t.getOrderSource());
		map.put("template", t.getTemplate());
		map.put("remark", t.getRemark());
		map.put("oneself_pickup_flg", t.getOneselfPickupFlg());
		map.put("name", t.getName());
		map.put("count", t.getCount());
		map.put("unit", t.getUnit());
		map.put("weight", t.getWeight());
		map.put("currency", t.getCurrency());
		map.put("amount", t.getAmount());
		map.put("source_area", t.getSourceArea());
		map.put("product_record_no", t.getProductRecordNo());
		map.put("good_prepard_no", t.getGoodPrepardNo());
		map.put("tax_no", t.getTaxNo());
		map.put("createdate", t.getCreatedate());
		map.put("sapobj", t.getSapobj());
		map.put("sapdoc", t.getSapdoc());
		map.put("wlgs", t.getWlgs());
		map.put("docstatus", t.getDocstatus());
		map.put("destcode", t.getDestcode());
		map.put("origincode", t.getOrigincode());
		map.put("errcode", t.getErrcode());
		map.put("result", t.getResult());
		return map;
	}
 	
 	/**
	 * 替换sql中的变量
	 * @param sql
	 * @param t
	 * @return
	 */
 	public String replaceVal(String sql,TWlApiEntity t){
 		sql  = sql.replace("#{id}",String.valueOf(t.getId()));
 		sql  = sql.replace("#{orderid}",String.valueOf(t.getOrderid()));
 		sql  = sql.replace("#{mailno}",String.valueOf(t.getMailno()));
 		sql  = sql.replace("#{is_gen_bill_no}",String.valueOf(t.getIsGenBillNo()));
 		sql  = sql.replace("#{jcompany}",String.valueOf(t.getJcompany()));
 		sql  = sql.replace("#{jcontact}",String.valueOf(t.getJcontact()));
 		sql  = sql.replace("#{jtel}",String.valueOf(t.getJtel()));
 		sql  = sql.replace("#{jmobile}",String.valueOf(t.getJmobile()));
 		sql  = sql.replace("#{jshippercode}",String.valueOf(t.getJshippercode()));
 		sql  = sql.replace("#{jcountry}",String.valueOf(t.getJcountry()));
 		sql  = sql.replace("#{jprovince}",String.valueOf(t.getJprovince()));
 		sql  = sql.replace("#{jcity}",String.valueOf(t.getJcity()));
 		sql  = sql.replace("#{jcounty}",String.valueOf(t.getJcounty()));
 		sql  = sql.replace("#{jaddress}",String.valueOf(t.getJaddress()));
 		sql  = sql.replace("#{jpost_code}",String.valueOf(t.getJpostCode()));
 		sql  = sql.replace("#{dcompany}",String.valueOf(t.getDcompany()));
 		sql  = sql.replace("#{dcontact}",String.valueOf(t.getDcontact()));
 		sql  = sql.replace("#{dtel}",String.valueOf(t.getDtel()));
 		sql  = sql.replace("#{dmobile}",String.valueOf(t.getDmobile()));
 		sql  = sql.replace("#{ddeliverycode}",String.valueOf(t.getDdeliverycode()));
 		sql  = sql.replace("#{dcountry}",String.valueOf(t.getDcountry()));
 		sql  = sql.replace("#{dprovince}",String.valueOf(t.getDprovince()));
 		sql  = sql.replace("#{dcity}",String.valueOf(t.getDcity()));
 		sql  = sql.replace("#{dcounty}",String.valueOf(t.getDcounty()));
 		sql  = sql.replace("#{daddress}",String.valueOf(t.getDaddress()));
 		sql  = sql.replace("#{dpost_code}",String.valueOf(t.getDpostCode()));
 		sql  = sql.replace("#{custid}",String.valueOf(t.getCustid()));
 		sql  = sql.replace("#{pay_method}",String.valueOf(t.getPayMethod()));
 		sql  = sql.replace("#{express_type}",String.valueOf(t.getExpressType()));
 		sql  = sql.replace("#{parcel_quantity}",String.valueOf(t.getParcelQuantity()));
 		sql  = sql.replace("#{cargo_length}",String.valueOf(t.getCargoLength()));
 		sql  = sql.replace("#{cargo_width}",String.valueOf(t.getCargoWidth()));
 		sql  = sql.replace("#{cargo_height}",String.valueOf(t.getCargoHeight()));
 		sql  = sql.replace("#{volume}",String.valueOf(t.getVolume()));
 		sql  = sql.replace("#{cargo_total_weight}",String.valueOf(t.getCargoTotalWeight()));
 		sql  = sql.replace("#{declared_value}",String.valueOf(t.getDeclaredValue()));
 		sql  = sql.replace("#{declared_value_currency}",String.valueOf(t.getDeclaredValueCurrency()));
 		sql  = sql.replace("#{customs_batchs}",String.valueOf(t.getCustomsBatchs()));
 		sql  = sql.replace("#{sendstarttime}",String.valueOf(t.getSendstarttime()));
 		sql  = sql.replace("#{is_docall}",String.valueOf(t.getIsDocall()));
 		sql  = sql.replace("#{need_return_tracking_no}",String.valueOf(t.getNeedReturnTrackingNo()));
 		sql  = sql.replace("#{return_tracking}",String.valueOf(t.getReturnTracking()));
 		sql  = sql.replace("#{d_tax_no}",String.valueOf(t.getDTaxNo()));
 		sql  = sql.replace("#{tax_pay_type}",String.valueOf(t.getTaxPayType()));
 		sql  = sql.replace("#{tax_set_accounts}",String.valueOf(t.getTaxSetAccounts()));
 		sql  = sql.replace("#{original_number}",String.valueOf(t.getOriginalNumber()));
 		sql  = sql.replace("#{payment_tool}",String.valueOf(t.getPaymentTool()));
 		sql  = sql.replace("#{payment_number}",String.valueOf(t.getPaymentNumber()));
 		sql  = sql.replace("#{goods_code}",String.valueOf(t.getGoodsCode()));
 		sql  = sql.replace("#{in_process_waybill_no}",String.valueOf(t.getInProcessWaybillNo()));
 		sql  = sql.replace("#{brand}",String.valueOf(t.getBrand()));
 		sql  = sql.replace("#{specifications}",String.valueOf(t.getSpecifications()));
 		sql  = sql.replace("#{temp_range}",String.valueOf(t.getTempRange()));
 		sql  = sql.replace("#{order_name}",String.valueOf(t.getOrderName()));
 		sql  = sql.replace("#{order_cert_type}",String.valueOf(t.getOrderCertType()));
 		sql  = sql.replace("#{order_cert_no}",String.valueOf(t.getOrderCertNo()));
 		sql  = sql.replace("#{order_source}",String.valueOf(t.getOrderSource()));
 		sql  = sql.replace("#{template}",String.valueOf(t.getTemplate()));
 		sql  = sql.replace("#{remark}",String.valueOf(t.getRemark()));
 		sql  = sql.replace("#{oneself_pickup_flg}",String.valueOf(t.getOneselfPickupFlg()));
 		sql  = sql.replace("#{name}",String.valueOf(t.getName()));
 		sql  = sql.replace("#{count}",String.valueOf(t.getCount()));
 		sql  = sql.replace("#{unit}",String.valueOf(t.getUnit()));
 		sql  = sql.replace("#{weight}",String.valueOf(t.getWeight()));
 		sql  = sql.replace("#{currency}",String.valueOf(t.getCurrency()));
 		sql  = sql.replace("#{amount}",String.valueOf(t.getAmount()));
 		sql  = sql.replace("#{source_area}",String.valueOf(t.getSourceArea()));
 		sql  = sql.replace("#{product_record_no}",String.valueOf(t.getProductRecordNo()));
 		sql  = sql.replace("#{good_prepard_no}",String.valueOf(t.getGoodPrepardNo()));
 		sql  = sql.replace("#{tax_no}",String.valueOf(t.getTaxNo()));
 		sql  = sql.replace("#{createdate}",String.valueOf(t.getCreatedate()));
 		sql  = sql.replace("#{sapobj}",String.valueOf(t.getSapobj()));
 		sql  = sql.replace("#{sapdoc}",String.valueOf(t.getSapdoc()));
 		sql  = sql.replace("#{wlgs}",String.valueOf(t.getWlgs()));
 		sql  = sql.replace("#{docstatus}",String.valueOf(t.getDocstatus()));
 		sql  = sql.replace("#{destcode}",String.valueOf(t.getDestcode()));
 		sql  = sql.replace("#{origincode}",String.valueOf(t.getOrigincode()));
 		sql  = sql.replace("#{errcode}",String.valueOf(t.getErrcode()));
 		sql  = sql.replace("#{result}",String.valueOf(t.getResult()));
 		sql  = sql.replace("#{UUID}",UUID.randomUUID().toString());
 		return sql;
 	}
 	
 	/**
	 * 执行JAVA增强
	 */
 	private void executeJavaExtend(String cgJavaType,String cgJavaValue,Map<String,Object> data) throws Exception {
 		if(StringUtil.isNotEmpty(cgJavaValue)){
			Object obj = null;
			try {
				if("class".equals(cgJavaType)){
					//因新增时已经校验了实例化是否可以成功，所以这块就不需要再做一次判断
					obj = MyClassLoader.getClassByScn(cgJavaValue).newInstance();
				}else if("spring".equals(cgJavaType)){
					obj = ApplicationContextUtil.getContext().getBean(cgJavaValue);
				}
				if(obj instanceof CgformEnhanceJavaInter){
					CgformEnhanceJavaInter javaInter = (CgformEnhanceJavaInter) obj;
					javaInter.execute("t_wl_api",data);
				}
			} catch (Exception e) {
				e.printStackTrace();
				throw new Exception("执行JAVA增强出现异常！");
			} 
		}
 	}
}